﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AzureProvider.ssdsClient;

namespace AzureProvider.SoupMethods
{

    public class Role
    {
        public string Id;
        public string RoleName;
    }

    public class RolesMethods : BaseMethods 
    {
        string errMessage;
        public static string Kind = "Role";

        protected override string containerId
        {
            get { return "UserContainer"; }
        }

        protected override string kind
        {
            get { return Kind; }
        }

        protected override Entity CreateEntity(object Object)
        {
            Role myobj = (Role)Object;

            var entity = new Entity()
            {
                Id = myobj.Id ,
                Kind = kind
            };

            entity.Properties = new Dictionary<string, object>();
            entity.Properties.Add("RoleName", myobj.RoleName);

            return entity;
        }

        public override object CreateObject(Entity Entity)
        {
            Role myObj = new Role();
            myObj.Id = Entity.Id;
            myObj.RoleName = (string)Entity.Properties.Single(tt => tt.Key == "RoleName").Value;

            return myObj;
        }

        public bool IsUserInRole(string UserName, string RoleName)
        {
            string errMessage;
            string qry = string.Format(
                @"from u in entities.OfKind(""{0}"") 
                    where  u[""UserName""]==""{3}""
                    from  ur in entities.OfKind(""{1}"") 
                    where ur[""UserID""] == u.Id  
                    from  r in entities.OfKind(""{2}"") 
                    where r[""RoleName""] == ""{4}"" && ur[""RoleID""] == r.Id 
                select r
                "
                , MembershipUserMethods.Kind                
                , UserRolesMethods.Kind
                , RolesMethods.Kind
                , UserName, RoleName);

            return (GetEntities(qry, out errMessage).Count >0 );
        }

        public Role GetRole(string RoleName, out string errMessage)
        {
            Role role = null;
            string qry = null;
            if (RoleName != null)
                qry = string.Format(" from e in entities where e.Kind == \"{0}\" && e[\"RoleName\"] == \"{1}\"  select e", kind, RoleName);
            if (GetEntities(qry, out errMessage).Count > 0)
                role = (Role)base.GetObjects(qry, out errMessage).First();

            return role;
        }

        public string[] GetUsersInRole(string RoleName)
        {
            
            string qry = string.Format(
                 @"from u in entities.OfKind(""{0}"") 
                    
                    from  ur in entities.OfKind(""{1}"") 
                    where ur[""UserID""] == u.Id  
                    from  r in entities.OfKind(""{2}"") 
                    where r[""RoleName""] == ""{3}"" && ur[""RoleID""] == r.Id 
                select u
                "
                , MembershipUserMethods.Kind
                , UserRolesMethods.Kind
                , RolesMethods.Kind
                , RoleName);

            return GetEntities(qry, out errMessage).Select(ee => (string)ee.Properties.Single(tt => tt.Key == "UserName").Value).ToArray<string>();

        }

        public  string[] FindUsersInRole(string roleName, string usernameToMatch)
        {
             
            string qry = string.Format(
                @"from u in entities.OfKind(""{0}"") 
                    where  u[""UserName""].Contains(""{3}"")
                    from  ur in entities.OfKind(""{1}"") 
                    where ur[""UserID""] == u.Id  
                    from  r in entities.OfKind(""{2}"") 
                    where r[""RoleName""] == ""{4}"" && ur[""RoleID""] == r.Id 
                select r
                "
                , MembershipUserMethods.Kind
                , UserRolesMethods.Kind
                , RolesMethods.Kind
                , usernameToMatch, roleName);

            return GetEntities(qry, out errMessage).Select(ee => (string)ee.Properties.Single(tt => tt.Key == "UserName").Value).ToArray<string>();
        }

        public  string[] GetAllRoles()
        {
            return GetObjects(null, out errMessage).Cast<Role>().Select(r => r.RoleName).ToArray();
        }
    }
}
